package com.cleaningcloud.cld.service.impl;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.cleaningcloud.cld.domain.CldPayDetails;
import com.cleaningcloud.cld.domain.CldRewardsDetails;
import com.cleaningcloud.cld.domain.dto.CldRewardsDto;
import com.cleaningcloud.cld.domain.vo.CldRewardsVo;
import com.cleaningcloud.cld.mapper.CldPayDetailsMapper;
import com.cleaningcloud.cld.service.ICldPayDetailsService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldRewardsMapper;
import com.cleaningcloud.cld.domain.CldRewards;
import com.cleaningcloud.cld.service.ICldRewardsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 奖惩管理Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2024-11-08
 */
@Service
public class CldRewardsServiceImpl implements ICldRewardsService 
{
    @Autowired
    private CldRewardsMapper cldRewardsMapper;

    @Autowired
    private ICldPayDetailsService cldPayDetailsService;

    /**
     * 查询奖惩管理
     * 
     * @param rewardsId 奖惩管理主键
     * @return 奖惩管理
     */
    @Override
    public CldRewardsVo selectCldRewardsByRewardsId(Long rewardsId)
    {
        return cldRewardsMapper.selectCldRewardsByRewardsId(rewardsId);
    }

    /**
     * 查询奖惩管理列表
     * 
     * @param cldRewards 奖惩管理
     * @return 奖惩管理
     */
    @Override
    public List<CldRewardsVo> selectCldRewardsList(CldRewardsDto cldRewards)
    {
        return cldRewardsMapper.selectCldRewardsList(cldRewards);
    }

    /**
     * 新增奖惩管理
     * 
     * @param cldRewards 奖惩管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldRewards(CldRewardsDto cldRewards)
    {
        cldRewards.setCreateTime(DateUtils.getNowDate());
        cldRewards.setFlag(0L);
        cldRewards.setCreateName(SecurityUtils.getUsername());
        cldRewards.setApproveStatus(1L);
        cldRewards.setCreateId(SecurityUtils.getUserId());
        if(cldRewards.getStaffList() != null && !cldRewards.getStaffList().isEmpty()){
            for (CldRewardsDetails cldRewardsDetails : cldRewards.getStaffList()) {
                cldRewards.setRewardsId(null);
                cldRewards.setStaffId(cldRewardsDetails.getStaffId());
                cldRewardsMapper.insertCldRewards(cldRewards);
            }
        }
        return 1;
    }

    /**
     * 修改奖惩管理
     * 
     * @param cldRewards 奖惩管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCldRewards(CldRewards cldRewards)
    {
        cldRewards.setApproveStatus(1L);
        return cldRewardsMapper.updateCldRewards(cldRewards);
    }

    @Override
    @Transactional
    public int auditCldRewards(CldRewards cldRewards) {
        CldRewardsVo cldRewardsVo = cldRewardsMapper.selectCldRewardsByRewardsId(cldRewards.getRewardsId());
        if(cldRewardsVo == null){
            throw new RuntimeException("奖惩记录查询失败");
        }
        cldRewards.setApproveId(SecurityUtils.getUserId());
        cldRewards.setApproveName(SecurityUtils.getUsername());
        cldRewards.setApproveTime(DateUtils.getNowDate());
        if(cldRewards.getApproveStatus() == 3){
            // 添加薪酬确认明细
            CldPayDetails cldPayDetails = new CldPayDetails();
            cldPayDetails.setBusinessId(cldRewardsVo.getRewardsId());
            cldPayDetails.setProjectId(cldRewardsVo.getProjectId());
            cldPayDetails.setStaffId(cldRewardsVo.getStaffId());
            cldPayDetails.setProjectPostId(cldRewardsVo.getProjectPostName());
            cldPayDetails.setPayType(5L);
            if(cldRewardsVo.getRewardsType() == 1){
                cldPayDetails.setRemark(cldRewards.getRemark() == null ? "" : cldRewards.getRemark()+"[奖励:+"+cldRewards.getRewardsMoney()+"]");
            }else{
                cldPayDetails.setRemark(cldRewards.getRemark() == null ? "" : cldRewards.getRemark()+"[惩罚:+"+cldRewards.getRewardsMoney()+"]");
            }
            cldPayDetails.setMoney(cldRewardsVo.getRewardsType() == 1 ? cldRewardsVo.getRewardsMoney() : cldRewardsVo.getRewardsMoney().negate());
            cldPayDetails.setProduceTime(DateUtils.getNowDate());
            // 1 奖励 2惩罚 奖惩类型
            cldPayDetails.setIsAlgorithm(cldRewardsVo.getRewardsType());
            cldPayDetailsService.insertCldPayDetails(cldPayDetails);
        }
        if(cldRewards.getApproveStatus() == 2){
            cldRewardsVo.setRejectTime(DateUtils.getNowDate());
        }
        cldRewardsMapper.updateCldRewards(cldRewards);
        return 1;
    }

    /**
     * 批量删除奖惩管理
     * 
     * @param rewardsIds 需要删除的奖惩管理主键
     * @return 结果
     */
    @Override
    public int deleteCldRewardsByRewardsIds(Long[] rewardsIds)
    {

        return cldRewardsMapper.deleteCldRewardsByRewardsIds(rewardsIds);
    }

    /**
     * 删除奖惩管理信息
     * 
     * @param rewardsId 奖惩管理主键
     * @return 结果
     */
    @Override
    public int deleteCldRewardsByRewardsId(Long rewardsId)
    {
        return cldRewardsMapper.deleteCldRewardsByRewardsId(rewardsId);
    }
}
